
;(function(){

    var __components = {},__plugins = {};

    var H5x = {        
        component:function(){         
            var module_name,
                pmodules=[],
                init,
                proto;

            for(var i=0;i<arguments.length;i++){
                var arg = arguments[i];
                if(!arg) return ;
                if(typeof arg == "string"){
                    module_name = arg;
                }else if(typeof arg == "function"){
                    init = arg;
                }else if(arg instanceof Array){
                    pmodules = arg;
                }else if(typeof arg == "object"){
                    proto = arg;
                }
            }

            //console.log();

            var M = function(){
                if(M.parents){
                    var self_ = this;
                    M.parents.forEach(function(P){
                        P.apply(self_,arguments);
                    });
                }

                if(typeof init == "function"){
                    init.apply(this,arguments);
                }
            };

            M.prototype["use"] = function(name,opts){
                return H5x.use(this,name,opts);
            };

            pmodules.forEach(function(pname){
                if(pmodule && __components[pmodule]){
                    this.extend(M,__components[pmodule]);
                }else{
                    console.error("not found component '"+pname+"'");
                }
            });

            for(var x in proto){
                M.prototype[x] = proto[x];
            }

            if(module_name){
                __components[module_name] = M;
            }
            
            return M;
        },

        plugin:function(name,init){
            __plugins[name] = init;
        },

        use:function(component,name,opts){
            //console.log(component);
            if(__plugins[name]){
                return __plugins[name](component,opts);
            }
        },

        extend:function(M,P){        
            if(P){
                if(!M.parents){
                    M.parents = [];
                }
                M.parents.push(P);
                for(var x in P.prototype){
                    M.prototype[x] = P.prototype[x];
                }
            }
        }
    };

    window.H5x = H5x;

    if(window.jQuery){
        window.jQuery.H5x = H5x;
        window.jQuery.fn.make = function(module_name,opts){
            var M = __components[module_name];
            if(!M){
                console.error("not found component '"+module_name+"'");
                return ;
            }
            opts = opts || {};
            opts.container = $(this);
            return new M(opts);
        }
    }
    
})();